#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
#include <stdexcept>
#include "../sdlmap.h"
#include "../sdlevent.h"
#include "../item.h"
#include "../mapitem.h"
#include "../astarque.h"
#include "../node.h"
#include "../astar.h"
#include "../character.h"
#include "../location.h"
#include "../itemfactory.h"
#include "../mapitemfactory.h"
#include "drawmap.h"
#include "createmap.h"

DrawMap::DrawMap(SDL_Surface *screen, Location *loca)
{
	screen_ = screen;
	loc_ = loca;
	map_ = new SDLMap(loc_, screen_);
	
	map_->render(screen_);
	SDL_Flip(screen_);
}

DrawMap::~DrawMap()
{
	delete map_;
}

void DrawMap::run(CreateMap *mapEditor)
{
	bool running = true;
	
	while(running)
	{
			
		event_.Poll();
		
		// Quit if user presses the escape key
		if(event_.GetKey() == "escape")
		{
			running = false;
		}
		
		if(event_.mouse_clicked() == true)
		{	
		     	
			// Convert the mouseX,Y coordinates to map coordinates
			int x = event_.GetMouseX();
			int y = event_.GetMouseY();
			map_->getTileXY(x, y);
			
			// Send the tile coordinate chosen back to editMap
			mapEditor->makeChange(x, y);
			
			running = false;
			
			draw();
		}
		
	}
}

void DrawMap::draw()
{
	SDLMap * garbage;
	garbage = map_;
	map_ = 0;
	delete garbage;
	map_ = new SDLMap(loc_, screen_);
	map_->render(screen_);
	SDL_Flip(screen_);
}

void DrawMap::setLocation(Location* newLoc)
{
	loc_ = newLoc;
	draw();
}
